package com.frxuexi.park2210.sign

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.SignBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.park2210.R
import com.frxuexi.park2210.adapter.SignAdapter
import com.frxuexi.park2210.databinding.ActivitySignListBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/app/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding,SignViewModel>() {
    val signAdapter by lazy { SignAdapter() }
    var month = Calendar.getInstance().get(Calendar.MONTH)+1
    override fun initData() {

        vm.getSignSuc.observe(this){
            val signBean = Gson().fromJson(it, SignBean::class.java)
                .filter {
                    if(month<10) {
                        vm.getYM(it.startTime) == "${Calendar.getInstance().get(Calendar.YEAR)}-0$month"
                    }else{
                        vm.getYM(it.startTime) == "${Calendar.getInstance().get(Calendar.YEAR)}-$month"
                    }
                }
            signAdapter.addData(signBean)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.rvMonth.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }

        binding.tvMonth.setText("${month}月")
        vm.getSigns(mapOf("month" to month))

        binding.ivLeft.setOnClickListener {
            if(month<=1){
                return@setOnClickListener
            }
            month-=1
            binding.tvMonth.setText("${month}月")
            signAdapter.data.clear()
            vm.getSigns(mapOf("month" to month))
        }

        binding.ivRight.setOnClickListener {
            if(month>=12){
                return@setOnClickListener
            }
            month+=1
            binding.tvMonth.setText("${month}月")
            signAdapter.data.clear()
            vm.getSigns(mapOf("month" to month))
        }
    }

}